<template>
  <v-chart class="chart" :option="options" />
</template>

<script setup lang="ts">
import { use } from 'echarts/core';
import { BarChart, LineChart } from 'echarts/charts';
import { TooltipComponent, ToolboxComponent, LegendComponent, GridComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import type { ComposeOption } from 'echarts/core';
import type { BarSeriesOption, LineSeriesOption } from 'echarts/charts';
import VChart, { THEME_KEY } from 'vue-echarts';
import type {
  TooltipComponentOption,
  ToolboxComponentOption,
  LegendComponentOption,
  GridComponentOption
} from 'echarts/components';
import { ref, provide } from 'vue';

use([TooltipComponent, ToolboxComponent, LegendComponent, GridComponent, BarChart, LineChart, CanvasRenderer]);

provide(THEME_KEY, 'light');

type EChartsOption = ComposeOption<
  | TooltipComponentOption
  | ToolboxComponentOption
  | LegendComponentOption
  | GridComponentOption
  | BarSeriesOption
  | LineSeriesOption
>;

const options = ref<EChartsOption>({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  toolbox: {
    feature: {
      // dataView: { show: true, readOnly: false },
      // magicType: { show: true, type: ['line', 'bar'] },
      // restore: { show: true },
      saveAsImage: { show: true }
    }
  },
  legend: {
    data: ['销售额', '订单数']
  },
  xAxis: [
    {
      type: 'category',
      data: [
        '20240-08-26',
        '2024-08-27',
        '2024-08-28',
        '2024-08-29',
        '2024-08-30',
        '2024-08-31',
        '2024-09-01',
        '2024-09-02',
        '2024-09-03'
      ],
      axisPointer: {
        type: 'shadow'
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '销售额',
      min: 0,
      max: 70,
      interval: 14,
      axisLabel: {
        formatter: '{value}'
      }
    },
    {
      type: 'value',
      name: '订单数',
      min: 0,
      max: 10,
      interval: 2,
      axisLabel: {
        formatter: '{value}'
      }
    }
  ],
  series: [
    {
      name: '销售额',
      type: 'bar',
      tooltip: {
        valueFormatter: function (value) {
          return value + ' $';
        }
      },
      data: [0, 0, 0, 42, 0, 0, 0, 0, 0]
    },
    {
      name: '订单数',
      type: 'line',
      yAxisIndex: 1,
      tooltip: {
        valueFormatter: function (value) {
          return value + ' 箱';
        }
      },
      data: [0, 0, 0, 2, 0, 0, 0, 0, 0]
    }
  ]
});
</script>

<style scoped>
.chart {
  height: 400px;
}
</style>
